//
//  VideoListCell.m
//  cooba-iOS
//
//  Created by 郭四虎 on 16/5/10.
//  Copyright © 2016年 YouKu. All rights reserved.
//

#import "VideoListCell.h"

@implementation VideoListCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    self.time.layer.cornerRadius = 10;
}

- (void)showModel:(VideoListModel *)model
{
    [self.ima sd_setImageWithURL:[NSURL URLWithString:model.img_url]placeholderImage:[UIImage imageNamed:@"酷吧LOGO_meitu_1"]];
    self.title.text = model.title;
    
    self.num.text = [NSString stringWithFormat:@"%@次播放",model.view];
    
    CGSize size = [self.title.text boundingRectWithSize:CGSizeMake(WIDTH-20, MAXFLOAT) options:(NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading)attributes:@{NSFontAttributeName:self.title.font} context:nil].size;
    self.titleWidth.constant = size.height + 5;
    
    if (model.duration) {
        self.time.text = [self getTime:model.duration];
    }else{
        [self.time removeFromSuperview];
    }
    
}

- (NSString *)getTime:(NSString *)str
{
    NSString *string;
    NSInteger duration = str.integerValue;
    NSString *h;
    NSString *m;
    NSString *s;
    
    //分钟不存在
    if (duration/60) {
        //小时
        if (duration/60/60) {
            if (duration/60/60>9) {
                h = [NSString stringWithFormat:@"%ld",duration/60/60];
            }else{
                h = [NSString stringWithFormat:@"0%ld",duration/60/60];
            }
            
            NSInteger left = duration-3600;
            if (left/60>9) {
                m = [NSString stringWithFormat:@"%ld",left/60];
            }else{
                m = [NSString stringWithFormat:@"0%ld",left/60];
            }
            if (left%60>9) {
                s = [NSString stringWithFormat:@"%ld",left%60];
            }else{
                s = [NSString stringWithFormat:@"0%ld",left%60];
            }
            
        }else{
            
            //分钟
            if (duration/60>9) {
                m = [NSString stringWithFormat:@"%ld",duration/60];
            }else{
                m = [NSString stringWithFormat:@"0%ld",duration/60];
            }
            if (duration%60>9) {
                s = [NSString stringWithFormat:@"%ld",duration%60];
            }else{
                s = [NSString stringWithFormat:@"0%ld",duration%60];
            }
        }
        
    }else{
        m = [NSString stringWithFormat:@"00"];
        if (duration%60>9) {
            s = [NSString stringWithFormat:@"%ld",duration%60];
        }else{
            s = [NSString stringWithFormat:@"0%ld",duration%60];
        }
    }
    if (h.integerValue) {
        self.timeWidth.constant = 75;
        string = [NSString stringWithFormat:@"%@:%@:%@",h,m,s];
    }else{
        self.timeWidth.constant = 50;
        string = [NSString stringWithFormat:@"%@:%@",m,s];
    }
    
    return string;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
